package service.resourceFileGet;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * @Description ${DESCRIPTION}.
 * @author: wwchen
 * @DateTime: 2018-05-12 16:30
 */

public class TestResource implements ApplicationContextAware {

    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void getResource() throws IOException{
        //如果不指定来源，则默认读取classpath，因为读取配置文件appCtx-base.xml就已经置顶了classpath路径
        Resource resource1 = applicationContext.getResource("classpath:file/resource.txt");
        Resource resource = applicationContext.getResource("url:https://www.baidu.com");
        ;
        System.out.println(resource1.getInputStream());
        System.out.println(resource.contentLength());
    }
}
